<?php
class Contact extends Zend_Form
{
	public function init() {
		$this->setMethod('post');

		$contact = new Zend_Session_Namespace('tender/contact');
		$user = Zend_Registry::get('Logged_User');

		$hours = new Zend_Form_Element_Select('hours', array('id'=>'hours'));
		$hours->setLabel('Получить предложения в течении: ')
			->addFilter('StripTags')
			->addFilter('StringTrim')
			->addValidator('Digits')
			->setRequired(true)
			;

		for ( $i = 2; $i < 25; $i++ ) {
			$hours->addMultiOption($i, $i.' часов');
		}

		if ( isset($contact->hours) ) {
			$hours->setValue($contact->hours);
		}
		
		$this->addElements(array($hours));

		if ( $user->id == Agitum_Model_User::GUEST_USER_ID )
		{
    		$prep = new Zend_Form_Element_Hidden('xxx');
    		$prep->setLabel('<h3>Как к вам обратиться</h3>');
    
    		$lastname = new Zend_Form_Element_Text('lastname', array('id' => 'lastname'));
    		$lastname->setLabel('Фамилия:')
    			->addFilter('StripTags')
    			->addFilter('StringTrim')
    			->addValidator('Alnum')
    			->setRequired(true)
    			;
    		if ( isset($contact->lastname) ) {
    			$lastname->setValue($contact->lastname);
    		}
    
    		$firstname = new Zend_Form_Element_Text('firstname', array('id' => 'firstname'));
    		$firstname->setLabel('Имя:')
    			->addFilter('StripTags')
    			->addFilter('StringTrim')
    			->addValidator('Alnum')
    			->setRequired(true)
    			;
    		if ( isset($contact->firstname) ) {
    			$firstname->setValue($contact->firstname);
    		}
    
    		$middlename = new Zend_Form_Element_Text('middlename', array('id' => 'middlename'));
    		$middlename->setLabel('Отчество:')
    			->addFilter('StripTags')
    			->addFilter('StringTrim')
    			->addValidator('Alnum')
    			->setRequired(true)
    			;
    		if ( isset($contact->middlename) ) {
    			$middlename->setValue($contact->middlename);
    		}
    
    		$phone = new Zend_Form_Element_Text('phone', array('id' => 'phone'));
    		$phone->setLabel('Телефон:')
    			->addFilter('StripTags')
    			->addFilter('StringTrim')
    			->addValidator('Regex', null, array(
    								'pattern'=> '/^\+?\d\s?\(?\d{3}\)?\s?\d{3}(?:\s|-)?\d{2}(?:\s|-)\d{2}?$/',
    								'messages' => array(
    									Zend_Validate_Regex::NOT_MATCH =>
    										"'%value%' неверный формат номера телефона.<br/>Требуется указать номер в формате: +8 (888) 888-88-88"
    									)
    								)
    							)
    			->addValidator('Db_NoRecordExists', null, array('users', 'name'))
    			->setRequired(true)
    			;
    		if ( isset($contact->phone) ) {
    			$phone->setValue($contact->phone);
    		}
    
    		$email = new Zend_Form_Element_Text('email', array('id' => 'email'));
    		$email->setLabel('E-mail:')
    			->addFilter('StripTags')
    			->addFilter('StringTrim')
    			->addValidator('EmailAddress')
    			->addValidator('Db_NoRecordExists', null, array('users', 'email'))
    			->setRequired(true)
    			;
    		if ( isset($contact->email) ) {
    			$email->setValue($contact->email);
    		}
    		$this->addElements(array($prep, $lastname, $firstname, $middlename, $phone, $email));
		}
		
		$sbmt = new Zend_Form_Element_Submit('place');
		$sbmt->setLabel('Разместить');

		$this->addElements(array($sbmt));

		foreach ( $this->getElements() as $element ) {
        	$element->setDecorators(
        		array(
		        	'ViewHelper',
        			'Errors',
		        	array(array('data' => 'HtmlTag'), array('tag' => 'div', 'id' => $element->getId().'-data')),
		        	array(array('label' => 'Label'), array('tag' => 'div', 'escape' => false)),
		        	array(array('row' => 'HtmlTag'), array('tag' => 'div', 'id' => $element->getId().'-element', 'class' => $element->class )),
	        	)
        	);
        }

		$sbmt->setDecorators(array('ViewHelper', array('data'=>'HtmlTag', 'tag'=>'div')));

        $this->setDecorators(array(
		    'FormElements',
		    array('HtmlTag', array('tag' => 'div', 'id' => 'tender-contact')),
		    'Form',
		));
	}
}